package List;

/**
 * 2.两数相加
 * 给你两个 非空 的链表，表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的，并且每个节点只能存储 一位 数字。
 * 请你将两个数相加，并以相同形式返回一个表示和的链表。
 * 你可以假设除了数字 0 之外，这两个数都不会以 0 开头。
 */
public class addTwoNumbers {
    /**
     * hot100一刷
     */
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode head = new ListNode();
        ListNode cur = head;
        int flag = 0;
        while (l1 != null && l2 != null) {
            int sum = l1.val + l2.val;
            if (flag == 1) {
                sum++;
            }

            if (sum >= 10) {
                cur.next = new ListNode(sum % 10);
                flag = 1;
            } else {
                cur.next = new ListNode(sum);
                flag = 0;
            }
            cur = cur.next;
            l1 = l1.next;
            l2 = l2.next;
        }

        if (flag != 1) {
            cur.next = l1 == null ? l2 : l1;
        } else {
            while (l1 != null) {
                int sum = l1.val;
                if (flag == 1) {
                    sum++;
                }
                if (sum >= 10) {
                    cur.next = new ListNode(sum % 10);
                    flag = 1;
                } else {
                    cur.next = new ListNode(sum);
                    flag = 0;
                }
                cur = cur.next;
                l1 = l1.next;
            }

            while (l2 != null) {
                int sum = l2.val;
                if (flag == 1) {
                    sum++;
                }

                if (sum >= 10) {
                    cur.next = new ListNode(sum % 10);
                    flag = 1;
                } else {
                    cur.next = new ListNode(sum);
                    flag = 0;
                }
                cur = cur.next;
                l2 = l2.next;
            }
        }
        if (flag == 1) {
            cur.next = new ListNode(1);
        }

        return head.next;
    }

    public ListNode addTwoNumbers2(ListNode l1, ListNode l2) {
        ListNode head = new ListNode();
        ListNode cur = head;
        while (l1 != null && l2 != null) {
            int sum = l1.val + l2.val;
            cur.next = new ListNode(sum);
            cur = cur.next;
            l1 = l1.next;
            l2 = l2.next;
        }
        cur.next = l1 == null ? l2 : l1;

        cur = head;
        int flag = 0;
        while (cur != null) {
            if (flag == 1) {
                cur.val += 1;
            }
            if (cur.val >= 10) {
                cur.val = cur.val % 10;
                flag = 1;
            } else {
                flag = 0;
            }
            if (cur.next == null && flag == 1) {
                cur.next = new ListNode(1);
            }
            cur = cur.next;
        }


        return head.next;
    }


    /**
     * hot100二刷
     */
    class Solution {
        boolean addOne = false;

        public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
            if (l1 == null && l2 == null) {
                return addOne ? new ListNode(1) : l1;
            }
            int v1 = l1 == null ? 0 : l1.val;
            int v2 = l2 == null ? 0 : l2.val;
            int val = v1 + v2;
            if (addOne)
                val++;
            if (val >= 10) {
                addOne = true;
            } else {
                addOne = false;
            }
            ListNode head = new ListNode(val % 10);
            if (l1 != null)
                l1 = l1.next;
            if (l2 != null)
                l2 = l2.next;
            head.next = addTwoNumbers(l1, l2);
            return head;
        }
    }

}
